<template>
  <div class="cart">
    <div class="cart-header">
      <span class="header-text"
        >共 &nbsp;<span class="allCount">{{ allCount }}</span> &nbsp;件</span
      >
    </div>
    <ul class="cart-list">
      <li class="cart-item" v-for="item in cartList" :key="item.id">
        <div>
          <span class="name">{{ item.name }}</span>
          <span class="price">{{ item.price }}￥</span>
          <span>
            <span class="count">{{ item.count }}</span>
            <button @click="delGoods(item.id)">-</button>
          </span>
        </div>
      </li>
    </ul>
    <div class="cart-footer">
      <el-button @click="clearCart">清空</el-button>
      <el-button type="primary" @click="submit">结账&nbsp;{{allPrice}}￥</el-button>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    cartList: {
      type: Array
    }
  },
  computed: {
    allCount () {
      return this.cartList.reduce((pre, i) => i.count + pre, 0)
    },
    allPrice () {
      return this.cartList.reduce((pre, i) => i.count * i.price + pre, 0)
    }
  },
  methods: {
    delGoods (id) {
      console.log(id)
      this.$emit('delGoodsFn', id)
    },
    clearCart () {
      this.$emit('clearCartFn')
    },
    submit () {
      this.$message.success('总价为 ' + this.allPrice + ' 元')
    }
  }
}
</script>

<style lang="scss" scoped>
.cart {
  position: relative;
  background-color: #bbe;
  height: 100%;
  .cart-header {
    height: 50px;
    line-height: 50px;
    display: flex;
    justify-content: right;
    border-bottom: 1px solid #ccc;
    .header-text {
      width: 30%;
      color: rgb(25, 137, 230);
      .allCount {
        color: orange;
      }
    }
  }
  .cart-list {
    padding: 5px 5px 40px;
    overflow: scroll;
    height: 510px;
    &::-webkit-scrollbar{
      width: 0;
      height: 0;
    }
    .cart-item div {
      background-color: rgb(142, 125, 219);
      padding: 0 20px;
      border-radius: 20px;
      height: 40px;
      line-height: 40px;
      width: 100%;
      display: flex;
      margin: 5px 0;
      justify-content: space-between;
      .name {
        font-size: 12px;
        width: 80px;
      }
      .price {
        font-size: 14px;
        color: rgb(117, 22, 22);
      }
      .count {
        display: inline-block;
        width: 30px;
        color: orange;
      }
      button {
        border-radius: 50%;
        width: 20px;
        height: 20px;
        text-align: center;
        font-weight: 700;
        color: red;
      }
    }
  }
  .cart-footer {
    position: absolute;
    bottom: 0;
    display: flex;
    width: 100%;
    background-color: #bbe;
    button{
      flex: 1;
      &:last-child{
        flex: 3;
      }
    }
  }
}
</style>
